// pages/goodsDetails/goodsDetails.js
var util = require('../../utils/util.js');
var wxParse = require('../../utils/wxParse/wxParse.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    max_price: 0,
    spnum: 1,
    unit_price:0,
    goods_imgs: [
      { url: '/images/product_banner.png' },
      { url: '/images/product_banner.png' },
      { url: '/images/product_banner.png' },
    ],
    aaa: { name: '规格1', specid: '5', stock: 0, max_price: '1000' },
    // alltime: [
    //   { name: '规格1', stock: 0, specid: '51', max_price: '1000' },
    //   { name: '规格1', stock: 0, specid: '53', max_price: '2000' },
    //   { name: '规格1', stock: 0, specid: '25', max_price: '3000' },
    //   { name: '规格1', stock: 0, specid: '54', max_price: '4000' },
    // ],
    // configure: [
    //   { type: "车载类型", value: "车载充电器" },
    //   { type: "插口", value: "双口" },
    //   { type: "适用机型", value: "苹果手机，三星手机，华为手机，小米手机" },
    //   { type: "风格", value: "商务" },
    //   { type: "原装配件", value: "小米原装" },
    //   { type: "售后服务", value: "全国联保" },
    //   { type: "产地", value: "中国" },
    //   { type: "备注", value: "官方标配" },
    //   { type: "编号", value: "XP-11122333" }
    // ],
    infor: [
      {
        max_price: '1122',
        name: '车载充电器',
        stock: '12'
      }
    ],
    gid:0,
    article:'',
    defaultAddress:[]
  },
  chooseAddress:function(){
    app.navTo('../myads/myads?type=2' + "&isT=1")
  
  },
  //加入购物车
  showjrinfo: function () {
    var that = this;
    that.setData({
      isshow: true
    })
  },
  showjrshop: function () {
    var that = this;
    that.setData({
      isshop: true
    })
  },
  kshover: function (e) {
    let that = this;
    var list = that.data.alltime;
    var id = e.currentTarget.dataset.index;
    that.setData({
      id: id,
      aaa: that.data.alltime[id]
    })
  },
  addnum: function () {
    var spnum = this.data.spnum;
    spnum++;
    this.setData({
      spnum: spnum
    })
  },
  cannum: function () {
    var spnum = this.data.spnum;
    spnum--;
    if (spnum <= 0) {
      spnum = 1;
    }
    this.setData({
      spnum: spnum
    })
  },
  add_buy: function (e) {
    this.setData({
      isshop: false
    })
    var spnum = this.data.spnum;//商品数量
    var gid = this.data.gid;
    var that = this;
    if (!that.data.defaultAddress){
      wx.showToast({
        title: '请先添加地址',
        icon:"none"
      })
      return
    }
    var addressS = that.data.defaultAddress.province + that.data.defaultAddress.city + that.data.defaultAddress.district + that.data.defaultAddress.address;
    util.commonRequest('exchageGood', 'post', { gid: gid, spnum: spnum, receiving_address: addressS}, function (res) {
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })

        that.goodsDetail(that.data.gid);
      }else{
        wx.showToast({
          icon: 'loading',
          title:data.msg,
        })
      }
    });

  },
  add_car: function (e) {
    this.setData({
      isshow: false
    })
  },
  tocloase: function () {
    this.setData({
      isshow: false
    })
  },
  toloasesjo: function () {
    this.setData({
      isshop: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    this.setData({
      gid:id
    })
  },
  goodsDetail: function (id) {
    var that = this;
    util.commonRequest('goodDetail', 'post', {gid:id}, function (res) {
      var data = res.data.data.data;
      if (res.data.code == 200) {
        that.setData({
          goods_imgs: data.goods_banner,
          name:data.name,
          max_price:data.jifen,
          unit_price: data.unit_price,
          goods_img: data.goods_img,
          stock: data.stock,
          defaultAddress:res.data.data.address

        })

        // var that = this;
        var article = data.detail;
        
        if(article == ''){
          article = "<p>暂无详情</p>";
        }
        wxParse.wxParse('article', 'html', article, that);

      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    this.goodsDetail(this.data.gid);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})